package se.webbzon.boltzmann.minigame.botany;

import java.awt.Rectangle;

import se.webbzon.boltzmann.configuration.Difficulty;
import se.webbzon.boltzmann.minigame.chemistry.CarbonDioxideMolecule;
import se.webbzon.boltzmann.minigame.chemistry.CarbonMolecule;
import se.webbzon.boltzmann.minigame.chemistry.HydrogenSulfideMolecule;
import se.webbzon.boltzmann.minigame.chemistry.NitriteMolecule;
import se.webbzon.boltzmann.minigame.chemistry.OxygenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SandBoxMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SulfateMolecule;
import se.webbzon.boltzmann.minigame.chemistry.WaterMolecule;
import se.webbzon.boltzmann.minigame.effect.SandBoxCloud;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxAnchor;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxContainer;
import se.webbzon.boltzmann.minigame.sandbox.ReadOnlySandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxAnchor;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxArrow;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxPicture;
import se.webbzon.boltzmann.resource.Resources;

public class CarbonCycle extends Cycle {
	
	/*============================================================
	Protected Methods
	============================================================*/

	@Override protected void onCreate(ReadOnlySandBox box) {
		Rectangle r = new Rectangle();
		box.getBoundary(r);
		double w = r.getWidth(), h = r.getHeight();
		
		/** Create instances **/
		DefaultSandBoxContainer c, co2, o2;
		c = new DefaultSandBoxContainer(SandBoxMolecule.class);
		c.setPosition(208, 168);
		co2 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		co2.setPosition(400, 56);
		o2 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		o2.setPosition(512, 168);

		SandBoxPicture plant, campfire, fire;
		plant = new SandBoxPicture(Resources.getImageSet("biology_plant"),50);
		plant.setPosition(64, 168);
		plant.setSize(128, 128);
		plant.setSpeed(0.05);
		campfire = new SandBoxPicture(Resources.getTextureSet("object_campfire_01"),60);
		campfire.setPosition(400, 232);
		campfire.setSize(64, 32);
		fire = new SandBoxPicture(Resources.getTextureSet("effect_fire_02"),50);
		fire.setPosition(400, 168);
		fire.setSize(96, 128);
		fire.setSpeed(0.5);
		
		
		SandBoxCloud cloud = new SandBoxCloud();
		cloud.setPosition(r.getCenterX(), 40);
		cloud.setSize(r.getWidth() - 96, 64);
		Earth earth = new Earth();
		earth.setSize(earth.getWidth(), 32);
		
		/** Create arrows and anchors **/
		SandBoxAnchor an1 = new DefaultSandBoxAnchor(64,56);
		
		SandBoxArrow[] arrows = {
				new SandBoxArrow(plant,c),
				new SandBoxArrow(c,fire),
				new SandBoxArrow(fire,co2),
				new SandBoxArrow(o2,fire),
				new SandBoxArrow(co2,plant),
		};
		arrows[4].addAnchor(an1);
		
		/** Add items to cycle **/
		SandBoxItem[] items = {
				c, co2, o2,
				plant, campfire, fire, cloud, earth
		};
		
		for (SandBoxItem item : items)
			addItem(item);
		for (SandBoxItem item : arrows)
			addItem(item);
		
		/** Create and add molecules **/
		SandBoxMolecule[] molecules = {
				new WaterMolecule(), new HydrogenSulfideMolecule(),
				new OxygenMolecule(), new CarbonDioxideMolecule(),
				new SulfateMolecule(), new CarbonMolecule(),
				new NitriteMolecule()
		};
		
		for (SandBoxMolecule m : molecules) {
			m.setPosition(	0.1 + 0.8 * w * Math.random(), 
							0.1 + 0.8 * h * Math.random());
			m.setLifetime(-1);
			addItem(m);
		}
		
		/** Add solutions **/
		addSolution(c,CarbonMolecule.class);
		addSolution(co2,CarbonDioxideMolecule.class);
		addSolution(o2,OxygenMolecule.class);
	}

	@Override protected void onDestroy(ReadOnlySandBox box) {
		clearSolution();
		clearItems();
	}

	@Override public Difficulty getDifficulty() {
		return Difficulty.EASY;
	}

}
